﻿var ScrambledEggs;
(function (ScrambledEggs) {
    (function (Modules) {
        var Job = (function () {
            function Job() {
                this._youngBasics = $("#youngbasics");
                this._jobs = $("#jobs");
                this._gotojob = $("#gotojob");
                this._activeJob = null;
                this._jobTickTimeout = null;
            }
            Job.prototype.initialize = function (gameState, onUpdate) {
                var me = this;
                me._onUpdate = onUpdate;
                me._youngBasics.find("button.findjob").click(function () {
                    me._youngBasics.hide();
                    me._jobs.show();
                    me._jobs.find(".other").click();
                });
                me._jobs.find(".cancel").click(function () {
                    me._youngBasics.show();
                    me._jobs.hide();
                });
                me._jobs.find(".other").click(function () {
                    var jobs = me._jobs.find(".job");
                    var job = null;
                    while(job == null || job.is(":visible")) {
                        job = $(jobs.get(ScrambledEggs.Utility.Number.Random(0, jobs.length)));
                    }
                    me._activeJob = ScrambledEggs.PartTimeJob[job.attr("data-job")];
                    jobs.not(job.show()).hide();
                });
                me._jobs.find(".apply").click(function () {
                    gameState.State.Message = "Let's see if you're up for the job...";
                    me._onUpdate();
                    me._youngBasics.hide();
                    var confirmJob = function () {
                        me._youngBasics.show();
                        me._jobs.hide();
                        gameState.State.PartTimeJob = me._activeJob;
                        me._onUpdate();
                    };
                    switch(me._activeJob) {
                        case ScrambledEggs.PartTimeJob.Banker:
                            ScrambledEggs.Utility.General.YesNoBox("Are you sure you want to become a banker?", function () {
                                ScrambledEggs.Utility.General.YesNoBox("Are you very sure? Banking is boring...", function () {
                                    ScrambledEggs.Utility.General.YesNoBox("So you really want to be a banker?", function () {
                                        ScrambledEggs.Utility.General.YesNoBox("Are you just clicking that button?", $.noop, function () {
                                            ScrambledEggs.Utility.General.OptionBox("Last chance to change your mind.", {
                                                Content: "I've changed my mind",
                                                Action: $.noop
                                            }, {
                                                Content: "Let me be a banker!",
                                                Action: confirmJob
                                            });
                                        });
                                    }, $.noop);
                                }, $.noop);
                            }, $.noop);
                            break;
                        case ScrambledEggs.PartTimeJob.Cleaning:
                            ScrambledEggs.Utility.General.YesNoBox("Are you sure you want to clean - I mean, tame - dragons?", function () {
                                ScrambledEggs.Utility.General.YesNoBox("And you do have experience with cleaning?", function () {
                                    ScrambledEggs.Utility.General.YesNoBox("Are you the sort of person that doens't mind being tricked?", function () {
                                        ScrambledEggs.Utility.General.YesNoBox("If this job had nothing to do with dragons, would be upset?", $.noop, function () {
                                            ScrambledEggs.Utility.General.OptionBox("Will you accept the job offer?", {
                                                Content: "I think I'll pass",
                                                Action: $.noop
                                            }, {
                                                Content: "Yes, I see nothing fishy with this offer and accept it!",
                                                Action: confirmJob
                                            });
                                        });
                                    }, $.noop);
                                }, $.noop);
                            }, $.noop);
                            break;
                        case ScrambledEggs.PartTimeJob.Manager:
                            ScrambledEggs.Utility.General.YesNoBox("Do you think you'd make a great manager?", function () {
                                ScrambledEggs.Utility.General.YesNoBox("Do you feel superior to most people in most ways?", function () {
                                    ScrambledEggs.Utility.General.YesNoBox("Are you sure you want to make heaps of money by doing nothin all day?", function () {
                                        ScrambledEggs.Utility.General.YesNoBox("If I asked you for a raise, would you give me one?", $.noop, function () {
                                            ScrambledEggs.Utility.General.OptionBox("I feel like you're overqualified, but if you want, you've got the job!", {
                                                Content: "I don't think managing is my thing after all.",
                                                Action: $.noop
                                            }, {
                                                Content: "Hand me the whip. I can start tomorrow!",
                                                Action: confirmJob
                                            });
                                        });
                                    }, $.noop);
                                }, $.noop);
                            }, $.noop);
                            break;
                        case ScrambledEggs.PartTimeJob.Research:
                            ScrambledEggs.Utility.General.YesNoBox("Are you generally interested in science?", function () {
                                ScrambledEggs.Utility.General.YesNoBox("Are you, generally speaking, an intelligent person?", function () {
                                    ScrambledEggs.Utility.General.YesNoBox("Are you resistant to all kinds of pain and discomfort?", function () {
                                        ScrambledEggs.Utility.General.YesNoBox("Would you agree that evolution is just a theory?", $.noop, function () {
                                            ScrambledEggs.Utility.General.OptionBox("Do you really feel like you're up for this, then?", {
                                                Content: "On second thought, I don't very much enjoy pain.",
                                                Action: $.noop
                                            }, {
                                                Content: "No pain, no gain! Let's do some science!",
                                                Action: confirmJob
                                            });
                                        });
                                    }, $.noop);
                                }, $.noop);
                            }, $.noop);
                            break;
                        case ScrambledEggs.PartTimeJob.Shoemaker:
                            ScrambledEggs.Utility.General.YesNoBox("Do you have any experience with leatherworking?", function () {
                                ScrambledEggs.Utility.General.YesNoBox("Do you desire to make the best shoe ever made?", function () {
                                    ScrambledEggs.Utility.General.YesNoBox("Have you always dreamt of exploring the universe wearing the ultimate footwear??", function () {
                                        ScrambledEggs.Utility.General.YesNoBox("Are you terrible at keeping secrets?", $.noop, function () {
                                            ScrambledEggs.Utility.General.OptionBox("Do you accept this job as a shoemaker?", {
                                                Content: "Actually, shoes are boring.",
                                                Action: $.noop
                                            }, {
                                                Content: "It's time to make some shoes!",
                                                Action: confirmJob
                                            });
                                        });
                                    }, $.noop);
                                }, $.noop);
                            }, $.noop);
                            break;
                    }
                });
                me._youngBasics.find("button.gotojob").click(function () {
                    switch(gameState.State.PartTimeJob) {
                        case ScrambledEggs.PartTimeJob.Banker:
                            gameState.State.JobCooldown = 160;
                            gameState.State.Message = "You stare at your watch, wishing you could go home soon.";
                            break;
                        case ScrambledEggs.PartTimeJob.Shoemaker:
                            gameState.State.Message = "You set out to find the materials required for shoe making.";
                            $.get(ScrambledEggs.Settings.Path.ShoemakerLevels[gameState.State.JobLevel], {
                            }, function (levelData) {
                                gameState.State.JobCooldown = 1;
                                me._onUpdate();
                                var adventure;
                                var glyphs = {
                                };
                                var playerGlyph = new ScrambledEggs.Glyph("ô", function () {
                                    return true;
                                }, gameState.State.Hat, "rgb(0, 200, 0)");
                                glyphs["*"] = new ScrambledEggs.Glyph("*", function () {
                                    return false;
                                }, "*", "rgb(0, 0, 200)");
                                glyphs["#"] = new ScrambledEggs.Glyph("#", function () {
                                    return true;
                                }, "#", "rgb(200, 0, 0)", function () {
                                    console.log("glyphs happen!");
                                });
                                adventure = new ScrambledEggs.Adventure(levelData, glyphs, playerGlyph, me._gotojob.find(".shoemaker").find("canvas").get(0));
                                adventure.Start();
                            }, "text");
                            break;
                        default:
                            ScrambledEggs.Utility.General.InfoBox("Job is not yet implemented");
                            break;
                    }
                    me._onUpdate();
                });
            };
            Job.prototype.update = function (gameState, triggerSource) {
                var me = this;
                if(gameState.State.Phase != ScrambledEggs.GamePhase.YoungAdult) {
                    me._youngBasics.find("button.findjob").hide();
                    return;
                }
                me._youngBasics.find("button.findjob").toggle(gameState.State.PartTimeJob == ScrambledEggs.PartTimeJob.None);
                me._youngBasics.find("button.gotojob").toggle(gameState.State.PartTimeJob != ScrambledEggs.PartTimeJob.None);
                if(gameState.State.JobCooldown <= 0) {
                    me._gotojob.hide();
                } else {
                    me._gotojob.show();
                    if(gameState.State.PartTimeJob == ScrambledEggs.PartTimeJob.Banker && !me._jobTickTimeout) {
                        me._jobTickTimeout = setTimeout(function () {
                            if(--gameState.State.JobCooldown <= 0) {
                                gameState.State.JobCooldown = 0;
                                gameState.State.Money += ScrambledEggs.Utility.Number.Random(2000, 2500);
                                gameState.State.Energy = 0;
                                gameState.State.Time -= 1;
                            }
                            me._jobTickTimeout = null;
                            me._onUpdate();
                        }, 1000);
                        me._gotojob.find(".banker").show();
                        var currentTime = 9 * 60 + (160 - gameState.State.JobCooldown) * 3;
                        var currentHour = Math.floor(currentTime / 60);
                        var currentMinute = currentTime - currentHour * 60;
                        var hourRep = currentHour < 10 ? "0" + currentHour : "" + currentHour;
                        var minuteRep = currentMinute < 10 ? "0" + currentMinute : "" + currentMinute;
                        me._gotojob.find(".banker .time").html(hourRep + " : " + minuteRep);
                    } else if(gameState.State.PartTimeJob == ScrambledEggs.PartTimeJob.Shoemaker) {
                        me._gotojob.find(".shoemaker").show();
                    }
                }
            };
            return Job;
        })();
        Modules.Job = Job;        
    })(ScrambledEggs.Modules || (ScrambledEggs.Modules = {}));
    var Modules = ScrambledEggs.Modules;
})(ScrambledEggs || (ScrambledEggs = {}));
